﻿var poll = {
    title: '',
    sapo: '',
    name: '',
    app_key: 'b88af5f35d5e00c948ae2bda2d867590',
    vote: function (ele) {
        var me = this;

        $boxVote = $(ele).parent().parent();

        if ($boxVote.find('input:checked').length > 0) {
            var voteID = '';
            $boxVote.find('input:checked').each(function (i) {
                if (i > 0)
                    voteID += ',';

                voteID += $(this).val();
            });

            $.ajax({
                url: '/Services/Vote/VoteHandler.ashx',
                data: ({ 'command': 'vote', 'VoteItemIDs': voteID, 'VoteID': $(ele).attr('voteid') }),
                success: function (response) {
                    //console.log(response);
                    response = eval('(' + response + ')');
                    if (response.Success)
                        me.view(ele);
                    else
                        alert('Bạn hãy bình chọn lại sau 3 phút nữa!');
                }
            });
        }
        else {
            alert('Bạn chưa chọn bình chọn nào!');
            return false;
        }
    },
    view: function (ele) {
        var me = this;
        if (me.title == '')
            me.title = $('.VCSortableInPreviewMode[type="vote"]').find('h4:eq(0)').text();
        if (me.sapo == '')
            me.sapo = $('.VCSortableInPreviewMode[type="vote"]').find('h4:eq(0)').next().text();
        //console.log(me);
        $('.popup-poll-content .p-title').html(me.title);
        $('.popup-poll-content .p-sapo').html(me.sapo);

        me.resize();
        me.openPopup();

        $.ajax({
            url: '/Services/Vote/VoteHandler.ashx',
            data: ({ 'command': 'viewresult', 'VoteID': $(ele).attr('voteid') }),
            error: function (e) {
            },
            success: function (response) {
                response = eval('(' + response + ')');
                if (response.Success) {
                    var data = response.Data.VoteAnswers;
                    result = '';
                    for (i = 0; i < data.length; i++) {
                        //console.log($("#VoteAnswer" + data[i].ID).next());
                        var value = $("#VoteAnswer" + data[i].ID).next().html();
                        result += '<li>';
                        result += String.format('<span class="p-vote-title">{0}</span>', value);
                        result += String.format('<span class="p-vote-percent"><span style="width:{0}%">{0}%</span></span>', data[i].VotePercent);
                        result += String.format('<span class="p-vote-result">{0} bình chọn</span>', data[i].VoteRate);
                        result += '</li>';
                    }
                    $('.popup-poll-content ul').html(result);
                    $('.popup-poll-content .p-vote-date').text(response.Data.StartDate);
                    me.title = response.Data.Title;
                    me.sapo = response.Data.Sapo;

                    me.bindData($(ele).attr('voteid'));
                }
            }
        });
    },
    bindData: function (voteID) {
        var me = this;

        $(window).resize(function () {
            me.resize();
        });

        $('#popup-poll-bg').scrollTop(0);
        $('.popup-poll-content li').removeClass('active');

        if (me.title != null)
            $('.popup-poll-right-top h3').text(me.title);
        if (me.sapo != null)
            $('.popup-poll-right-top p').text(me.sapo);

        var MingWidth = '100%';
        var MingAppKey = me.app_key;
        var MingTitle = $.base64Encode(encodeURI(me.title));
        var MingUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
        var MingNumCount = 5;
        var MingDebug = 1;
        var MingMin = 0; //1: hien thi box comment, 0: hien thi list comment
        var MingScroll = 1;
        var MingBox = 'mingid_comments_content_pollpopup';
        var MingiframeId = 'mingId_frame_polllightbox';

        $('#mingid_comments_content_pollpopup').empty();
        mingid_comment_iframe_height = me.adBoxHeight();
        MINGID_IFRAME_FUNC.mingidGenIfram(MingWidth, mingid_comment_iframe_height, MingAppKey, MingUrl, MingTitle, MingNumCount, MingDebug, MingMin, MingScroll, MingUrl, MingBox, MingiframeId);

        $('#popup-poll-wrap').click(function () { return false; });
        $('#popup-poll-bg').click(function () { me.closePopup(); });
    },
    openPopup: function () {
        $('#popup-poll-bg').fadeIn('slow');
        $('body').addClass('no-scroll');
        $('#scroll_top').hide();

        $(document).keyup(this.keyUp);
    },
    closePopup: function () {
        $('#popup-poll-bg').fadeOut('slow');
        $('body').removeClass('no-scroll');

        $(document).off('keyup');
    },
    resize: function () {
        var minPhotoContainer = 280;
        var minWindowWidth = 600;
        var minWindowHieght = 400;

        var windowWidth = window.innerWidth - 150;
        var windowHeight = window.innerHeight - 150;
        windowWidth = (windowWidth < minWindowWidth) ? minWindowWidth : windowWidth;
        windowHeight = (windowHeight < minWindowHieght) ? minWindowHieght : windowHeight;

        var commentWidth = 320;
        var paddingBox = 56;
        var marginLeft = 15;

        var photoContainerWidth = windowWidth - $('#popup-poll-right').innerWidth();
        photoContainerWidth = (photoContainerWidth < minPhotoContainer) ? minPhotoContainer : photoContainerWidth;

        $('#popup-poll-wrap').css({ 'width': windowWidth });
        $('#popup-poll-left, .popup-poll-content').css({ 'width': photoContainerWidth - paddingBox - marginLeft, 'height': windowHeight });
        $('#popup-poll-right').css('height', windowHeight);

        var wWidth = document.documentElement.clientWidth;
        var wHeight = document.documentElement.clientHeight;
        var popupWidth = $('#popup-poll-wrap').width();
        var popupHeight = $('#popup-poll-wrap').height();
        $('#popup-poll-wrap').css({ 'left': wWidth / 2 - popupWidth / 2 - 10 });

        _height = windowHeight - $('.popup-poll-content .p-text').innerHeight() - $('.popup-poll-content .p-title').innerHeight() - $('.popup-poll-content .p-sapo').innerHeight() - $('.popup-poll-content .p-vote-date').innerHeight();
        $('.popup-poll-content ul').slimScroll({ height: _height, opacity: '1', alwaysVisible: false });

        //Ming Height
        MingHeight = this.adBoxHeight();
        if (MingHeight <= 150)
            MingHeight = 150;

        $('#mingId_frame_quiz_lightbox').attr('height', MingHeight);
    },
    keyUp: function (e) {
        e.preventDefault();
        if (e.keyCode == 27) { poll.closePopup(); }
    },
    adBoxHeight: function () {
        var height = $('#popup-poll-right').height() - $('.popup-poll-right-top').innerHeight();
        return height;
    }
}
